package com.wbc.utils;


/**
 * @author wangbingchen
 * @Description
 * @create 2021-11-23 14:12
 * 数字转中文工具
 *
 */
public class DigitalToChineseUtil {
    private DigitalToChineseUtil(){

    }

    //零、壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿

    //兆 仟 佰 拾 亿 仟 佰 拾 万 仟 佰 拾 元
//        String str = "〇一二三四五六七八九十";

    private static String Monad = "千百十兆千百十亿千百十万千百十元";
    private static String theStandbyNumType = "〇一二三四五六七八九";

    private static String defaultMonad = "仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元";
    private static String defaultNumType = "零壹贰叁肆伍陆柒捌玖";
    private static String LING = defaultNumType.charAt(0)+"";


    /**
     * 对外方法
     * @param num
     * @return
     */
    public static String formatChinese(String num) {
        if(num.indexOf('.')==-1){
            return formatChineseInt(num)+"整";
        }
        String[] split = num.split("\\.");
        if("00".equals(split[1])){
            return formatChineseInt(split[0])+"整";
        }
        String intNum = formatChineseInt(split[0]);
        String fNum = formatChineseDecimals(split[1]);
        return intNum+fNum;
    }

    /**
     * 添加单位
     * @param num
     * @return
     */
    private static String addTheUnit(String num) {
        StringBuilder sb = new StringBuilder();
        String str = defaultMonad;
        str = str.substring(str.length()-num.length());
        for (int i = 0; i < num.length(); i++) {
            // 元 万 亿 兆 节点单位之前的零需要移动到单位后
            String node = ""+num.charAt(i);
            String lastNode = i>0?""+num.charAt(i-1):LING;
            if((num.length()-1-i)%4==0&&LING.equals(node)){
                sb.append(str.charAt(i));
                sb.append(num.charAt(i));
            }else if(LING.equals(node)&&!LING.equals(lastNode)){
                sb.append(num.charAt(i));
            }else if(LING.equals(node)&&LING.equals(lastNode)){
                //什么都不用做
            }else {
                sb.append(num.charAt(i));
                sb.append(str.charAt(i));
            }

        }
        // 处理 零
        return theFinalProcessing(sb.toString());
    }

    private static String theFinalProcessing(String num) {
        //壹仟零万零元零整
        String replace = num.replace(LING+"兆", "兆")
                .replace(LING+"亿", "亿")
                .replace(LING+"万", "万")
                .replace(LING+"元", "元");
        // 不能 零结尾
        int i = replace.lastIndexOf(LING);
        if(i==replace.length()-1){
            replace = replace.substring(0,i);
        }
        return replace;
    }

    /**
     * 整数转换
     * @param num
     * @return
     */
    private static String formatChineseInt(String num) {
        StringBuilder sb = new StringBuilder();
        char[] chars = num.toCharArray();
        for (char aChar : chars) {
            char c = defaultNumType.charAt(aChar - 48);
            sb.append(c);
        }

        return addTheUnit(sb.toString());
    }

    /**
     * 小数转换 传入一个两位数 不是只取左边两位
     * @param num 示例 03
     * @return
     */
    private static String formatChineseDecimals(String num) {
        // 1  10
        if("00".equals(num)){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(defaultNumType.charAt(num.charAt(0) - 48));
        if(num.charAt(0)!=48){
            sb.append("角");
        }
        if(num.length()>1){
            sb.append(defaultNumType.charAt(num.charAt(1) - 48));
            if(num.charAt(1)!=48){
                sb.append("分");
            }
        }
        return sb.toString();
    }


    // 测试
    public static void main(String[] args) {
        System.out.println(formatChinese("10.1")); //叁佰捌拾贰元壹角贰分
        System.out.println(formatChinese("123.21")); //壹佰贰拾叁元贰角壹分
    }

}
